home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Mac OS USB DDK_v1.0.1 / Examples / PrinterClassDriver / usbprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-03  |  7.6 KB  |  325 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        usbprint.c
  3.  
  4.     Contains:    usb printer class device communication
  5.                     (installed in UnitTable)
  6.  
  7. */
  8. #include "PrinterClassDriver.h"
  9.  
  10. #ifndef __DEVICES__
  11. #include <devices.h>
  12. #endif
  13.  
  14. #ifndef __FILES__
  15. #include <files.h>
  16. #endif
  17.  
  18. #define kMaskLowByte    0x0FF
  19.  
  20. extern pascal OSErr    DRVRDone( OSErr err, DCtlPtr ctl, IOParamPtr pb );
  21. static void AbortActive( CntrlParam *pb, DCtlPtr clt );
  22.  
  23. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24.     Name:        GetPrinterStruct
  25.  
  26.     Input Parameters:    IOParamPtr        i/o parameter block
  27.         
  28.     Output Parameters:
  29.         usbPrinterPBStruct    * pointer to the device
  30.         
  31.     Description:
  32.         Given a device control block, map it to a usb device's data structure
  33.  
  34.     Change History:
  35.         28 Feb 1998,    oja:        Original version.
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  37. static struct usbPrinterPBStruct    *
  38. GetPrinterStruct( DCtlPtr ctl )
  39. {
  40.     return (struct usbPrinterPBStruct *) ctl->dCtlStorage;
  41. }
  42.  
  43. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44.     Name:        Read
  45.  
  46.     Input Parameters:
  47.         
  48.     Output Parameters:
  49.         <none>
  50.         
  51.     Description:
  52.         
  53.     Change History:
  54.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->in.usbStatus
  55.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  56.         28 Feb 1998,    oja:        Original version.
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  58. static OSErr
  59. Read (IOParamPtr pb, DCtlPtr ctl)
  60. {
  61.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  62.  
  63.     //
  64.     //    if we have a unidirectional interface
  65.     //        report a read error
  66.     //        (only status supported by unidirectional is Centronics compatible)
  67.     //
  68.     pb->ioResult = paramErr;    // assume bad
  69.  
  70.     if ( usbprint->interface->interfaceProtocol == kUSBPrintClassProtocolUnidirectional )
  71.         pb->ioResult = readErr;
  72.     else if ( usbprint != NULL )
  73.         (*usbprint->qread)( pb, ctl, usbprint );    //    map the read param block into the usb param block
  74.  
  75.     return pb->ioResult;
  76.  
  77. }
  78.  
  79. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80.     Name:        Write
  81.  
  82.     Input Parameters:
  83.         
  84.     Output Parameters:
  85.         <none>
  86.         
  87.     Description:
  88.         
  89.     Change History:
  90.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->out.usbStatus
  91.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  92.         28 Feb 1998,    oja:        Original version.
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  94. static OSErr
  95. Write (IOParamPtr pb, DCtlPtr ctl)
  96. {
  97.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  98.  
  99.     pb->ioResult = paramErr;    // assume bad
  100.  
  101.     if ( usbprint != NULL )
  102.         (*usbprint->qwrite)( pb, ctl, usbprint );    //    map the write param block into the usb param block
  103.  
  104.     return pb->ioResult;
  105. }
  106.  
  107. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108.     Name:        AbortActive
  109.  
  110.     Input Parameters:
  111.         pb
  112.         ctl
  113.         
  114.     Output Parameters:
  115.         <none>
  116.         
  117.     Description:
  118.         
  119.     Change History:
  120.         4 Aug 1998,    oja:        Original version.
  121. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  122. static void
  123. AbortActive( CntrlParam *pb, DCtlPtr ctl )
  124. {
  125.     //        we need to wait here until the transaction is complete
  126.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  127.     struct USBPB                    *pActiveUSBIO;
  128.     IOParamPtr                        pActiveIO;
  129.  
  130.     if ( usbprint != NULL )
  131.     {
  132.         if ( pb->ioCRefNum == usbprint->outRefNum )
  133.         {
  134.             pActiveUSBIO = &usbprint->out;
  135.             pActiveIO = usbprint->writeDrvr.pb;
  136.         }
  137.         else
  138.         {
  139.             pActiveUSBIO = &usbprint->in;
  140.             pActiveIO = usbprint->readDrvr.pb;
  141.         }
  142.         if ( pActiveUSBIO->usbCompletion != NULL )
  143.         {
  144.             (*usbprint->qabort)( pb->ioCRefNum, usbprint );    //    cancel outstanding transactions
  145.             //
  146.             //    synchronize data toggle
  147.             // USB addendum, the endpoints of the pipe may be out of sync
  148.             //    a soft reset in the printer class should restore the data toggle
  149.             //
  150.             pb->csCode = kDrvrSoftReset;
  151.             (*usbprint->qstatus)( pb, ctl, usbprint );
  152.  
  153.         }
  154.     }
  155. }
  156.  
  157. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158.     Name:        DRVROpen
  159.  
  160.     Input Parameters:
  161.         
  162.     Output Parameters:
  163.         <none>
  164.         
  165.     Description:
  166.         
  167.     Change History:
  168.         28 Feb 1998,    oja:        Original version.
  169. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  170.  
  171. pascal OSErr
  172. DRVROpen(CntrlParam *pb, DCtlPtr dce)
  173. {
  174.     return noErr;
  175. }
  176.  
  177. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178.     Name:        DRVRClose
  179.  
  180.     Input Parameters:
  181.         
  182.     Output Parameters:
  183.         <none>
  184.         
  185.     Description:
  186.         
  187.     Change History:
  188.         28 Feb 1998,    oja:        Original version.
  189. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  190.  
  191. pascal OSErr
  192. DRVRClose(CntrlParam *pb, DCtlPtr ctl)
  193. {
  194.     return noErr;
  195. }
  196.  
  197. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  198.     Name:        DRVRStatus
  199.  
  200.     Input Parameters:
  201.         
  202.     Output Parameters:
  203.         <none>
  204.         
  205.     Description:
  206.         
  207.     Change History:
  208.         30 Jun 1998,    oja:        pass through messages for kDrvrCentronicsStatus,
  209.                                         kDrvr1284IdString,kDrvrSoftReset
  210.         28 Feb 1998,    oja:        Original version.
  211. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  212.  
  213. pascal OSErr
  214. DRVRStatus(CntrlParam *pb, DCtlPtr ctl)
  215. {
  216.     OSErr                err;
  217.     struct             usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  218.  
  219.     switch ( pb->csCode )
  220.     {
  221.     case kDrvrCentronicsStatus:    //  USB device: centronics status
  222.     case kDrvr1284IdString:     //  USB device: 1284 capability string
  223.     case kDrvrSoftReset:    //  USB device: soft reset
  224.         if ( usbprint != NULL )
  225.             (*usbprint->qstatus)( pb, ctl, usbprint );    //    map the status param block into the usb param block
  226.         err = pb->ioResult;
  227.         break;
  228.     case kDrvrNumDevices:    
  229.         err =  noErr;
  230.         break;
  231.     case 1:    
  232.     case 2:    // deprecated
  233.     default:
  234.         err = paramErr;
  235.         break;
  236.     }
  237.     return err;
  238. }
  239.  
  240. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  241.     Name:        DRVRControl
  242.  
  243.     Input Parameters:
  244.         csCode                        csParam
  245.         ------                        -------
  246.         kDrvrPrivateSetStorage    pointer to the USB device class storage
  247.  
  248.     Output Parameters:
  249.         <none>
  250.         
  251.     Description:
  252.         
  253.     Change History:
  254.         12 Jun 1998,    oja:        default to noErr (was uninitialized with killCode)
  255.         11 Jun 1998,    oja:        abort active usb transactions
  256.          4 Apr 1998,    oja        Removed unused code for private done i/o
  257.                                          pass error code to DrvrDone
  258.         28 Feb 1998,    oja:        Original version.
  259. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  260.  
  261. pascal OSErr
  262. DRVRControl(CntrlParam *pb, DCtlPtr ctl)
  263. {
  264.     OSErr                                err            = noErr;
  265.     struct usbPrinterPBStruct    *usbprint    = GetPrinterStruct( ctl );
  266.  
  267.     switch ( pb->csCode )
  268.     {
  269.     case killCode:
  270.         //
  271.         //    killIO is always handled as an immediate mode transaction
  272.         //
  273.         AbortActive( pb, ctl );
  274.         break;
  275.     case kDrvrPrivateSetStorage:
  276.         //
  277.         //    reference the class driver's private storage
  278.         //        it's not a handle, but devices.h thinks it should be
  279.         //
  280.         ctl->dCtlStorage = (Handle) *((Ptr *) &pb->csParam[0] );
  281.         break;
  282.     default:
  283.         err = paramErr;
  284.         break;
  285.     }
  286.     return err;
  287. }
  288.  
  289. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  290.     Name:        DRVRPrime
  291.  
  292.     Input Parameters:
  293.         
  294.     Output Parameters:
  295.         <none>
  296.         
  297.     Description:
  298.         
  299.     Change History:
  300.         28 Feb 1998,    oja:        Original version.
  301. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  302.  
  303. pascal OSErr
  304. DRVRPrime(CntrlParam *pb, DCtlPtr ctl)
  305. {
  306.     OSErr    err = paramErr;
  307.     //
  308.     //    switch on the low order byte to dispatch reads and writes
  309.     //
  310.     if ( (pb->ioTrap & kMaskLowByte) == aRdCmd )
  311.         err = Read( (IOParamPtr) pb, ctl );
  312.     else if ( (pb->ioTrap & kMaskLowByte) == aWrCmd )
  313.         err = Write( (IOParamPtr) pb, ctl );
  314.  
  315.     //
  316.     //    get the ioResult in case the completion routine has already executed
  317.     //    
  318.     if ( err == noErr )
  319.         err = pb->ioResult;
  320.  
  321.     return err;
  322. }
  323.  
  324. // eof
  325.